package com.hs.android.fortress.net;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.message.BasicNameValuePair;
import com.google.android.gms.maps.model.LatLng;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

public class PositionService extends Service implements LocationListener{

	private static final String TAG = PositionService.class.getSimpleName();
	
	private LocationManager mLocationManager;
	private PostMethodCommand mCommand;
	private String mUserId;
	private LatLng lastPos;

	
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		
		SharedPreferences pref = getSharedPreferences("client", MODE_PRIVATE);
		mUserId = pref.getString("id", null);
		
		Log.d(TAG,"mUserId : " + mUserId);
		backgroundWork(mUserId);
		
		return Service.START_STICKY;
	}
	
	
	private void backgroundWork(String userId){
		setUpLocationManagerIfNeeded();
		
		mUserId = userId;
		mCommand = new FormPostCommand("/playersetting/position", null);
	}
	
	private void setUpLocationManagerIfNeeded() {
        if(mLocationManager == null){
        	mLocationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
        	mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, this);
        }
    }
	
	
	@Override
	public void onLocationChanged(Location location) {
		
	  	lastPos = new LatLng(location.getLatitude(), location.getLongitude());
	  	  	
	  	new PositionSetter().start();
	}

	
	private class PositionSetter extends Thread{
		
		@Override
		public void run() {
			if(lastPos != null){
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				params.add(new BasicNameValuePair("id", mUserId));
				params.add(new BasicNameValuePair("lat", ""+lastPos.latitude));
				params.add(new BasicNameValuePair("log", ""+lastPos.longitude));
				
				mCommand.setParams(params);
				
				try {
					HttpResponse response = mCommand.callMethod();
					// status 200 means "OK" - the request has succeeded
					if(response.getStatusLine().getStatusCode() != 200) {
						Log.d(TAG,"Status: "+response.getStatusLine().getStatusCode());
						return;
					}
					
					Log.d(TAG,"Status: "+response.getStatusLine().getStatusCode() );
				} catch (ClientProtocolException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}


	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	};

}
